<?php
/**
 * 公共包含文件,负责程序初始化 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: common.php 556 2011-05-09 11:46:35Z lisijie86@gmail.com $
*/

@set_magic_quotes_runtime(0);
function_exists('date_default_timezone_set') && date_default_timezone_set('Etc/GMT+0');

require 'defined.php';

define('DEBUG', true);
if ( DEBUG ) {
	error_reporting(E_ALL ^ E_NOTICE);
	$_DEBUG = array();
} else {
	error_reporting(0);
}
$starttime = explode(' ', microtime());
$starttime = $starttime[0] + $starttime[1];
require JBLOG_ROOT.'config.php';

require JBLOG_INC.'version.php';
require JBLOG_INC.'func_global.php';
require JBLOG_INC.'func_plugin.php';
require JBLOG_INC.'func_filter.php';

if ( version_compare(PHP_VERSION, '5.2.0', '<') ) {
	require JBLOG_INC.'func_compat.php';
}

if ( !get_magic_quotes_gpc() ) {
	$_GET = add_slashes($_GET);
	$_POST = add_slashes($_POST);
	$_COOKIE = add_slashes($_COOKIE);
}
$_SERVER = add_slashes($_SERVER);

!$_SERVER['PHP_SELF'] && $_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
if ( $_SERVER['REQUEST_URI'] ) {
	$temp = urldecode($_SERVER['REQUEST_URI']);
	if ( strpos($temp, '<') !== false || strpos($temp, '"') !== false ) {
		die('Request Forbidden!');
	}
	unset($temp);
}
if ( isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS']) ) {
	die('Request Forbidden!');
}

require JBLOG_INC.'class_mysql.php';
require JBLOG_INC.'class_rewrite.php';
require JBLOG_INC.'class_cache.php';
require JBLOG_INC.'class_widget.php';
require JBLOG_INC.'class_i18n.php';
require JBLOG_INC.'func_user.php';

$_CACHE = array();

$db = new Mysql();
$db->connect(DB_HOST, DB_USER, DB_PWD, DB_NAME, DB_PCONNECT);

$cache = new Cache(array('dir'=>'cache'));

get_cache(array('config','cate','usergroup'));

$_USER = array('id'=>0, 'name'=>'', 'group'=>'guest');
$_USER['refer'] = (empty($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) === FALSE) ? '' : $_SERVER['HTTP_REFERER'];
if ( strpos($_USER['refer'],'#') !== false ) {
	$_USER['refer'] = substr($_USER['refer'], 0, strpos($_USER['refer'],'#'));
}
$_USER['ip'] = get_onlineip();

check_user();

$_USER['permission'] = isset($_CACHE['usergroup'][$_USER['group']]) ? $_CACHE['usergroup'][$_USER['group']] : array();

//var_dump($_USER['permission']);

define('THEME_ROOT', JBLOG_ROOT.'themes'.DS.config('theme').DS);

check_blog_status();

if ( !config('cachemode') ) {
	$_CACHE['config']['cachemode'] = array();
} else {
	$_CACHE['config']['cachemode'] = explode('|',$_CACHE['config']['cachemode']);
}

$_CACHE['config']['blogurl'] = str_replace('{host}', $_SERVER['HTTP_HOST'], $_CACHE['config']['blogurl']);
$_CACHE['config']['blogdir'] = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'],'/')+1); //JBlog所在网站目录

$rewrite = new Rewrite();

//加载Widget
$widgets = config('widgets');
if ( $widgets ) {
	get_cache('widget');
	$widgets = unserialize($widgets);
	foreach ( $widgets as $value ) {
		$value = str_replace('.','',$value); 
		@include_once JBLOG_ROOT.'plugin'.DS.'widget'.DS.$value.'.php';
	}
}
unset($widgets);
	
//加载插件
if ( config('plugin_enabled') ) {
	$plugin_enabled = unserialize(config('plugin_enabled'));
	foreach ( $plugin_enabled as $key => $val ) {
		include_once JBLOG_ROOT.'plugin'.DS.$val;
	}
}

obstart();
?>